import 'package:extended_text_field/extended_text_field.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/models/entity/osc_user.dart';
import 'package:osc/models/pop_result/pop_result.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/pages/tweet/tweet_pick_friend.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/special_text/my_special_text_span_builder.dart';

typedef CommentInputContentCallback({@required bool submit, @required String content});

class ProjectCommentInputWidget extends StatefulWidget {
  final String content;
  final FocusNode focusNode;
  final CommentInputContentCallback commentInputContentCallback;

  const ProjectCommentInputWidget({Key key, @required this.focusNode, @required this.commentInputContentCallback, this.content = ""}) : super(key: key);

  @override
  _ProjectCommentInputWidgetState createState() => _ProjectCommentInputWidgetState(content, focusNode, commentInputContentCallback);
}

class _ProjectCommentInputWidgetState extends State<ProjectCommentInputWidget> {
  final String content;
  final FocusNode focusNode;
  final CommentInputContentCallback commentInputContentCallback;
  _ProjectCommentInputWidgetState(this.content, this.focusNode, this.commentInputContentCallback);

  TextEditingController editingController;
  bool report = false;
  int textLength = 0;
  int maxTextLength = 160;

  List<OscUser> users;

  @override
  void initState() {
    super.initState();
    editingController = TextEditingController(text: content);
  }


  @override
  Widget build(BuildContext context) {
    double padding = Dimens.gap_view_big;
    return Container(
      height: ScreenUtil.getScreenH(context),
      width: ScreenUtil.getScreenW(context),
      color: Colors.black38,
      child: Column(
        children: <Widget>[
          Expanded(child: Text("")),
          Container(
            color: Colors.white,
            padding: EdgeInsets.only(left: padding, right: padding, top:padding, bottom: padding),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  height: 120,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.black26, width: Dimens.gap_divider_height_smallest),
                  ),
                  child: buildExtendedTextField(),
                ),
                Row(
                  children: <Widget>[
                    IconButton(
                      onPressed: (){
                        NavigatorUtil.pushPageForResult<PopResult>(context, TweetPickFriends()).then((result){
                          users = result.extract["friends"];
                          if(users != null){
                            for(OscUser user in users){
                              editingController.text += " @${user.name} ";
                            }
                            refreshView();
                          }
                        });
                      },
                      icon: Padding(
                        padding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_normal),
                        child: Image.asset(PdUtil.getImagePath("ic_tweet_mention_normal")),
                      ),
                    ),
                    InkWell(
                      onTap: (){
                        setState(() {
                          report = !report;
                        });
                      },
                      child: Row(
                        children: <Widget>[
                          Image.asset(PdUtil.getImagePath(report? "form_checkbox_checked" : "form_checkbox_normal"), width: Dimens.view_item_icon_height_big, fit: BoxFit.contain,),
                          Text("转发到猿圈", style: report? MyTextStyle.CHECKBOX_CHECKED : MyTextStyle.CHECKBOX_UNCHECKED,),
                        ],
                      ),
                    ),
                    Expanded(
                      child: Text("$textLength/$maxTextLength", style: MyTextStyle.LIST_BODY, textAlign: TextAlign.end,),
                    ),
                    Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(left: Dimens.gap_view_normal),
                      height: Dimens.view_button_height_small,
                      width: Dimens.view_button_width_small,
                      decoration: BoxDecoration(
                        color: Colors.green,
                        borderRadius: BorderRadius.circular(Dimens.view_radius_size_small),
                      ),
                      child: InkWell(
                        onTap: (){
                          submit();
                        },
                        child: Text("确定", style: MyTextStyle.BUTTON,),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  buildExtendedTextField(){
    return ExtendedTextField(
      style: MyTextStyle.TWEET_CONTENT,
      maxLines: 10,
      focusNode: focusNode,
      controller: editingController,
      keyboardType: TextInputType.text,
      maxLength: 160,
      inputFormatters: [LengthLimitingTextInputFormatter(160)],
      decoration: InputDecoration(
        hintText: "请输入内容",
        hintStyle: MyTextStyle.HINT,
        contentPadding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_normal, vertical: Dimens.gap_view_normal),
        border: buildInputBorder(),
        disabledBorder: buildInputBorder(),
        enabledBorder: buildInputBorder(),
        errorBorder: buildInputBorder(),
        focusedBorder: buildInputBorder(),
        focusedErrorBorder: buildInputBorder(),
      ),
      specialTextSpanBuilder: MySpecialTextSpanBuilder(simpleAtTopicEnable: true),
      textInputAction: TextInputAction.send,
      onSubmitted: (text){
        submit();
      },
    );
  }

  buildInputBorder(){
    return InputBorder.none;
  }

  submit(){
    if(commentInputContentCallback != null){
      commentInputContentCallback(submit: true, content: editingController.text);
      editingController.text = "";
      textLength = 0;
      setState(() {

      });
    }
  }


  refreshView(){
    if(mounted){
      setState(() {

      });
    }
  }
}
